﻿using Abp;
using Abp.Application.Services;
using FwProject.Manager;
using FwProject.ShopApplication;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Abp.ObjectMapping;
using FwProject.ShoppingMall.EventAdvertising;
using Abp.Domain.Repositories;
using X.PagedList;
using System.Linq;
using AutoMapper.QueryableExtensions;
using Abp.AutoMapper;
using Abp.Authorization;

namespace FwProject.EventAdvertising.Dto
{
    /// <summary>
    /// 广告位管理
    /// </summary>
    [AbpAuthorize]
    public class ShopAdLocationAppService : FwProjectAppServiceBase, IShopAdLocationAppService
    {
        private readonly ShopAdLocationManager _shopAdLocationManager;
        private readonly IRepository<ShopAdLocation, Guid> _shopAdLocationRepository;
        private  Abp.IGuidGenerator _guid { get; set; }
        public IObjectMapper _map { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="shopAdLocationManager"></param>
        /// <param name="shopAdLocationRepository"></param>
        public ShopAdLocationAppService(ShopAdLocationManager shopAdLocationManager, IRepository<ShopAdLocation, Guid> shopAdLocationRepository) {

            _shopAdLocationManager = shopAdLocationManager;
            _guid = SequentialGuidGenerator.Instance;
            _map = NullObjectMapper.Instance;
            _shopAdLocationRepository = shopAdLocationRepository;
        }
        /// <summary>
        /// 创建一个广告位
        /// </summary>
        /// <param name="Input">模型</param>
        /// <returns></returns>
        public async Task<Guid> CreateShopAdLocation(ShopAdLocationInput Input)
        {
            var model = Input.MapTo<ShopAdLocation>();
            model.Id = _guid.Create();
            return await _shopAdLocationManager.CreateShopAdLocation(model);

        }
        /// <summary>
        /// 修改一个广告位
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public async Task EditShopAdLocation(Guid Id, ShopAdLocationInput Input)
        {
              var model = _shopAdLocationRepository.Get(Id);
             model = Input.MapTo(model);
             await _shopAdLocationManager.UpdateShopAdLocation(model);
        }
        /// <summary>
        /// 删除一个广告位
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task DeleteAdLocation(Guid Id)
        {
            var model = _shopAdLocationRepository.Get(Id);
            await _shopAdLocationManager.DeleteShopAdLocation(model);
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="pageSize">条数</param>
        /// <param name="adTypeLocation">广告位类别</param>
        /// <returns></returns>
        public async Task<IPagedList<ShopAdLocationDto>> GetPageList(int page,int pageSize, AdTypeLocation? adTypeLocation) {

            var list = _shopAdLocationRepository.GetAll();
            if (adTypeLocation != null)
                list = list.Where(o => o.AdTypeLocation == adTypeLocation);

            return  await list.OrderByDescending(o => o.CreationTime).ProjectTo<ShopAdLocationDto>().ToPagedListAsync(page, pageSize);
        }

        #region 后台管理
        /// <summary>
        /// 后台管理-获取所有广告位置（添加广告时使用）
        /// </summary>
        /// <returns></returns>
        [AbpAuthorize]
        public async Task<List<ShopAdLocationDto>> GetListAsync()
        {
            var list = await _shopAdLocationRepository.GetAll().OrderByDescending(o => o.CreationTime).ProjectTo<ShopAdLocationDto>().ToListAsync();
            return list;
        }

        /// <summary>
        /// 后台管理-获取广告位输入模型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AbpAuthorize]
        public async Task<ShopAdLocationInput> GetEnterDtoAsync(Guid id)
        {
            var entity = await _shopAdLocationRepository.GetAsync(id);

            return entity.MapTo<ShopAdLocationInput>();
        }

        /// <summary>
        /// 后台管理-广告位管理列表（Layui）
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        [AbpAuthorize]
        public async Task<LayerDataBaseDto> GetShopAdLocationLayerDataAsync(int page, int limit,string keyword)
        {
            var query = _shopAdLocationRepository.GetAll();
            if (!string.IsNullOrEmpty(keyword))
            {
                query = query.Where(o => o.AdLocationTitle.Contains(keyword));
            }

            var pagelist = await query.OrderByDescending(o=>o.CreationTime).ProjectTo<ShopAdLocationDto>().ToPagedListAsync(page, limit);

            LayerDataBaseDto dto = new LayerDataBaseDto();
            dto.Data = pagelist;
            dto.Count = pagelist.TotalItemCount;
            return dto;
        }
        #endregion
    }
}
